package com.reform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.reform.config.JwtTokenUtil;
import com.reform.pojo.admin.Admin;
import com.reform.mapper.AdminMapper;
import com.reform.res.Result;
import com.reform.res.ResultCode;
import com.reform.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reform.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mortale
 * @since 2021-06-26
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    RedisUtils redisUtils;
    @Autowired
    JwtTokenUtil jwtTokenUtil;

    /**
     * 获取管理员对于管理的部门的id
     *
     * @param gh
     * @return
     */
    @Override
    public Integer getAdminDepId(String gh) {
        return adminMapper.selectOne(new QueryWrapper<Admin>().eq("gh", gh)).getDepId();
    }


    /**
     * 转让超级管理员权限
     *
     * @param gh
     * @param request
     * @return
     */
    @Override
    public Result transferSuperAuth(String gh, HttpServletRequest request) {
        String token = request.getHeader("token");
        Integer typeFromToken = jwtTokenUtil.getTypeFromToken(token);
        if (typeFromToken != 3) {
            return Result.failure(ResultCode.USER_AUTH_FAIL);
        }
        if (adminMapper.selectOne(new QueryWrapper<Admin>().eq("gh", gh)) != null) {
            return Result.failure(ResultCode.USER_HAS_ADMINED);
        }
        adminMapper.delete(new QueryWrapper<Admin>().eq("dep_id", 0).eq("type", 1));
        //仅超级管理员，暨学校操作管理员可以转让权限
        int insert = adminMapper.insert(new Admin(gh, 0, 1));
        if (insert > 0) {
            return Result.success(ResultCode.SUCCESS);
        }
        return Result.failure(ResultCode.FAILURE);
    }

    @Override
    public boolean chargeAdminAuth(String gh) {
        Admin admin = adminMapper.selectOne(new QueryWrapper<Admin>().eq("gh", gh));
        if (admin == null) {
            return false;
        }
        return true;
    }

    /**
     * 根据工号获取admin
     *
     * @param gh
     * @return
     */
    @Override
    public Admin getAdminByGh(String gh) {
        return adminMapper.selectOne(new QueryWrapper<Admin>().eq("gh", gh));
    }

    @Override
    public Result setDepartmentAdmin(Admin admin) {
        Admin oldAdmin = adminMapper.selectOne(new QueryWrapper<Admin>().eq("gh", admin.getGh()));
        if (oldAdmin == null) {
            int insert;
            try {
                insert = adminMapper.insert(admin);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.failure(ResultCode.DEP_HAVE_ADMINED);
            }
            if (insert > 0) {
                redisUtils.del("teacherInfoList");
                return Result.success(ResultCode.SUCCESS);
            } else {
                return Result.failure(ResultCode.FAILURE);
            }
        } else {
            int delete = adminMapper.delete(new QueryWrapper<Admin>().eq("gh", admin.getGh()));
            if (delete > 0) {
                redisUtils.del("teacherInfoList");
                return Result.success(ResultCode.SUCCESS);
            } else {
                return Result.failure(ResultCode.FAILURE);
            }
        }
    }
}
